<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改店铺套餐')" />
</head>
<body>
<form class="layui-form" th:object="${shopCombo}">
    <div class="mainBox">
        <div class="main-container">
            <input name="id" th:field="*{id}" type="hidden">
    <div class="layui-form-item">
        <label class="layui-form-label">套餐名称：</label>
        <div class="layui-input-block">
            <input type="text" name="name" th:field="*{name}" placeholder="请输入套餐名称" class="layui-input"  lay-verify="required"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">充值金额：</label>
        <div class="layui-input-block">
            <input type="text" name="rechargeAmount" th:field="*{rechargeAmount}" placeholder="请输入充值金额" class="layui-input"  lay-verify="required"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">适用商品：</label>
        <div class="layui-input-block">
            <select name="goodsId"  lay-verify="required" dict-code="input" th:attr="default-value=*{goodsId}" id="goodsId">
                <option value="">所有</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">使用次数：</label>
        <div class="layui-input-block">
            <input type="text" name="times" th:field="*{times}" placeholder="请输入使用次数" class="layui-input" oninput="value=value.replace(/[^\d]/g,'')"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">有效天数：</label>
        <div class="layui-input-block">
            <input type="text" name="validDate" th:field="*{validDate}" placeholder="请输入有效天数" class="layui-input"  lay-verify="required" oninput="value=value.replace(/[^\d]/g,'')"/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注：</label>
        <div class="layui-input-block">
            <textarea name="comboRemark"  placeholder="请输入备注" class="layui-textarea">[[*{comboRemark}]]</textarea>
        </div>
    </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="combo-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
</body>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    layui.use(['form', 'jquery','laydate','dictionary'], function () {
        let form = layui.form;
        let $ = layui.jquery;
        let laydate = layui.laydate;

        let prefix = "/shop/combo/";

        form.on('submit(combo-update)', function (data) {
            for(var key in data.field){
                var type=$(data.form).find("input[name='"+key+"']").attr("type");
                if(type=="checkbox"){
                    var value="";
                    $(data.form).find("input[name='"+key+"']:checked").each(function(){
                        value+=$(this).val()+",";
                    })
                    if(value!=""){
                        value=value.substr(0,value.length-1);
                        data.field[key]=value;
                    }

                }
            }
            $.ajax({
                url: prefix+'update',
                data: JSON.stringify(data.field),
                dataType: 'json',
                contentType: 'application/json',
                type: 'put',
                success: function (result) {
                    if (result.success) {
                        layer.msg(result.msg, {icon: 1, time: 1000}, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.layui.table.reload("combo-table");
                        });
                    } else {
                        layer.msg(result.msg, {icon: 2, time: 1000});
                    }
                }
            })
            return false;
        });
    });

    $(function(){
        $.ajax({
            type: "GET",
            url: "/shop/goods/data/all",
            dataType:"json",
            async: true,    //如果设置为false，则表示异步提交，否则表示同步提交
            timeout: 10000,
            error: function(){
                alert('Error loading XML document');
                return  null;
            },
            success: function(data){
                var items = data.data;
                var inner = "";
                $.each(items, function(i, n){
                    if ($("#goodsId").attr("default-value") == items[i].id) {
                        inner += "<option value="+items[i].id+" selected>"+items[i].name+"</option>";
                    } else {
                        inner += "<option value="+items[i].id+">"+items[i].name+"</option>";
                    }
                });

                $("#goodsId").html(inner);
            }
        });

    })
</script>
</html>